package com.yuelan.codelib.download;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * class name：SQLiteOpenHelper<BR>
 * class description�?BR> PS�?<BR>
 * 
 * @version 1.00 2011/09/21
 * @author CODYY)peijiangping
 */

public class TpDB extends SQLiteOpenHelper {
	public final static int Index_Name = 1; // [name]
	public final static int Index_Id = 2; // [gameid]
	public final static int Index_Type = 3; // [type]
	public final static int Index_Url = 4; // [url]
	public final static int Index_State = 5; // [state] 1:下载完成 0:等待下载 -1:下载失败
	public final static int Index_DownFileSize = 6; // [downloadfilesize] 下载了多少
	public final static int Index_Filepath = 7; // [filepath] apkDir
	public final static int Index_FileType = 8; // [filetype]
	public final static int Index_IconUrl = 9; // [iconurl]
	public final static int Index_AdId = 10; // [adid]
	public final static int Index_PackageName = 11; // [packagename]
	public final static int Index_Popuptype = 12; // [popuptype]
	public final static int Index_Popuptext = 13; // [popupText]
	public final static int Index_DummySize = 14; // [appsize]
	public final static int Index_DownedSize = 15; // [totalFileSize]
	public final static int Index_PageId = 16; // [pageid]
	public final static int Index_Crc32 = 17; // [crc32]
	public final static int Index_DlCallBackUrl = 18; // [dlcallbackurl]

	private final static String DATEBASE_NAME = "dm_he.db";
	private final static int DATABASE_VERSION = 1;
	public final static String TABLE_NAME = "download_info";

	public TpDB(Context context) {
		super(context, DATEBASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE "
				+ TABLE_NAME
				+ "(_id INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT,"
				+ "name TEXT NOT NULL," + "gameid TEXT NOT NULL,"
				+ "type TEXT NOT NULL," + "url TEXT," + "state TEXT,"
				+ "downloadfilesize TEXT," + "filepath TEXT NOT NULL,"
				+ "filetype TEXT," + "iconurl TEXT," + "adid TEXT,"
				+ "packagename TEXT," + "popuptype TEXT," + "popupText TEXT,"
				+ "appsize TEXT," + "totalFileSize TEXT," + "pageid TEXT,"
				+ "crc32 TEXT," + "dlcallbackurl TEXT)";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
		db.execSQL(sql);
		onCreate(db);
	}

	// public void delete(String tableName, String id) {
	// String sql = "delete from " + tableName + "where gameid=" + id;
	// SQLiteDatabase db = this.getReadableDatabase();
	// db.execSQL(sql);
	// }

	/**
	 * Role:执行增删改查的sql语句，args为插入的数据数组<BR>
	 * Date:2012-4-9<BR>
	 * 
	 * @author ZHENSHI)peijiangping
	 */
	public void execSQL(String sql, Object[] args) {
		SQLiteDatabase db = this.getReadableDatabase();
		db.execSQL(sql, args);
	}

	/**
	 * 作用：删除表数据<BR>
	 * 作�?：peijiangping<BR>
	 * 日期�?012-12-5
	 */
	public void deleteAll(String tablename) {
		String sql = "delete from " + tablename;
		SQLiteDatabase db = this.getReadableDatabase();
		db.execSQL(sql);
		// 使自增的id值归�?
		String sql_ = "update sqlite_sequence set seq=0 where name='"
				+ tablename + "'";
		db.execSQL(sql_);

	}

	/**
	 * Role:sqlite事务执行<BR>
	 * Date:2012-4-20<BR>
	 * 
	 * @author ZHENSHI)peijiangping
	 */
	public void shiwuSQL(String[] sqlarray) {
		SQLiteDatabase db = this.getReadableDatabase();
		db.beginTransaction();// �?��事务
		try {
			for (int i = 0; i < sqlarray.length; i++) {
				db.execSQL(sqlarray[i]);
			}
			db.setTransactionSuccessful();// 调用此方法会在执行到endTransaction()
											// 时提交当前事务，如果不调用此方法会回滚事�?
		} finally {
			db.endTransaction();// 由事务的标志决定是提交事务，还是回滚事务
		}
	}

	public TpInfo getDownloadInfo(String packageName) {
		String sql_ = "select * from " + TABLE_NAME + " where packagename = ?";
		Cursor cusor = query(sql_, new String[] { packageName });
		if (cusor != null && cusor.getCount() > 0) {
			cusor.moveToNext();
			TpInfo info = new TpInfo();
			info.setId(cusor.getString(2));
			info.setName(cusor.getString(1));
			info.setPackageName(cusor.getString(11));
			info.setAdid(Integer.valueOf(cusor.getString(10)));
			info.setIconUrl(cusor.getString(Index_IconUrl));
			info.setPageid(Integer.valueOf(cusor.getString(16)));
			cusor.close();
			return info;
		}
		return null;
	}

	public TpInfo getDownloadInfoForId(String id, String packagename) {
		String sql = "select * from " + TABLE_NAME
				+ " where gameid = ? and packagename = ? ";
		Cursor cusor = query(sql, new String[] { id, packagename });
		if (cusor != null && cusor.getCount() > 0) {
			cusor.moveToNext();
			TpInfo info = new TpInfo();
			info.setId(cusor.getString(2));
			info.setName(cusor.getString(1));
			info.setPackageName(cusor.getString(11));
			info.setAdid(Integer.valueOf(cusor.getString(10)));
			info.setIconUrl(cusor.getString(Index_IconUrl));
			info.setPageid(Integer.valueOf(cusor.getString(16)));
			info.setDownloadFileSize(cusor.getString(Index_DownFileSize));
			info.setTotalFileSize(cusor.getString(Index_DownedSize));
			info.setAppDummySize(cusor.getString(Index_DummySize));
			info.setType(Integer.parseInt(cusor.getString(Index_Type)));
			cusor.close();
			return info;
		}
		return null;
	}

	public String getAppId(String packageName) {
		String sql_ = "select * from " + TABLE_NAME + " where packagename = ?";
		Cursor cusor = query(sql_, new String[] { packageName });
		if (cusor != null && cusor.getCount() > 0) {
			cusor.moveToNext();
			String appid = cusor.getString(2);
			return appid;
		}
		return null;
	}

	/**
	 * Role:查询数据库，返回cursor<BR>
	 * Date:2012-4-10<BR>
	 * 
	 * @author ZHENSHI)peijiangping
	 */
	public Cursor query(String sql, String[] args) {
		SQLiteDatabase db = this.getReadableDatabase();
		return db.rawQuery(sql, args);
	}

	public Cursor query_desc() {
		SQLiteDatabase db = this.getReadableDatabase();
		return db.query(TABLE_NAME, null, null, null, null, null, "_id desc");
	}
}
